package com.maxafaa.contrademo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.res.AssetManager;
import java.io.InputStream;
import android.view.MotionEvent;

public class MainSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private Paint m_paint;
	private Thread m_mainthread;
	private static int s_screen_width, s_screen_height;
	private Canvas m_lockcanvas;
	private AssetManager m_assetmanager;
	private InputStream m_inputstream;
	//mc
	private Animation[] m_mc_animations;
	private int m_mc_posx, m_mc_posy;
	private int m_mc_current_action;
	private boolean m_mc_is_face_left;
	private boolean m_mc_is_firing;
	private Bitmap m_mc_bitmap;
	
	//constance action
	//main action
	private final int M_ACTION_STANDING = 0;
	private final int M_ACTION_STANDING_UP = M_ACTION_STANDING + 1;
	private final int M_ACTION_RUNNING = M_ACTION_STANDING_UP + 1;
	private final int M_ACTION_RUNNING_UP = M_ACTION_RUNNING + 1;
	private final int M_ACTION_RUNNING_DOWN = M_ACTION_RUNNING_UP + 1;
	private final int M_ACTION_JUMPING = M_ACTION_RUNNING_DOWN + 1;
	private final int M_ACTION_LYING = M_ACTION_JUMPING + 1;
	private final int M_ACTION_DIEING = M_ACTION_LYING + 1;	//6
	
	//specific action
	private final int M_ACTION_STANDING_LEFT = 0;
	private final int M_ACTION_STANDING_RIGHT = M_ACTION_STANDING_LEFT + 1;
	private final int M_ACTION_STANDING_UP_LEFT = M_ACTION_STANDING_RIGHT + 1;
	private final int M_ACTION_STANDING_UP_RIGHT = M_ACTION_STANDING_UP_LEFT + 1;
	private final int M_ACTION_RUNNING_LEFT = M_ACTION_STANDING_UP_RIGHT + 1;
	private final int M_ACTION_RUNNING_RIGHT = M_ACTION_RUNNING_LEFT + 1;
	private final int M_ACTION_RUNNING_UP_LEFT = M_ACTION_RUNNING_RIGHT + 1;
	private final int M_ACTION_RUNNING_UP_RIGHT = M_ACTION_RUNNING_UP_LEFT + 1;
	private final int M_ACTION_RUNNING_DOWN_LEFT = M_ACTION_RUNNING_UP_RIGHT + 1;
	private final int M_ACTION_RUNNING_DOWN_RIGHT = M_ACTION_RUNNING_DOWN_LEFT + 1;
	private final int M_ACTION_JUMPING_LEFT = M_ACTION_RUNNING_DOWN_RIGHT + 1;
	private final int M_ACTION_JUMPING_RIGHT = M_ACTION_JUMPING_LEFT + 1;
	private final int M_ACTION_LYING_LEFT = M_ACTION_JUMPING_RIGHT + 1;
	private final int M_ACTION_LYING_RIGHT = M_ACTION_LYING_LEFT + 1;
	private final int M_ACTION_DIEING_LEFT = M_ACTION_LYING_RIGHT + 1;
	private final int M_ACTION_DIEING_RIGHT = M_ACTION_DIEING_LEFT + 1;//15
	
	private final int M_ACTION_STANDING_LEFT_SHOOT = M_ACTION_DIEING_RIGHT + 1;	
	private final int M_ACTION_STANDING_RIGHT_SHOOT = M_ACTION_STANDING_LEFT_SHOOT + 1;
	private final int M_ACTION_STANDING_UP_LEFT_SHOOT = M_ACTION_STANDING_RIGHT_SHOOT + 1;
	private final int M_ACTION_STANDING_UP_RIGHT_SHOOT = M_ACTION_STANDING_UP_LEFT_SHOOT + 1;
	private final int M_ACTION_RUNNING_LEFT_SHOOT = M_ACTION_STANDING_UP_RIGHT_SHOOT + 1;
	private final int M_ACTION_RUNNING_RIGHT_SHOOT = M_ACTION_RUNNING_LEFT_SHOOT + 1;
	private final int M_ACTION_RUNNING_UP_LEFT_SHOOT = M_ACTION_RUNNING_RIGHT_SHOOT + 1;
	private final int M_ACTION_RUNNING_UP_RIGHT_SHOOT = M_ACTION_RUNNING_UP_LEFT_SHOOT + 1;
	private final int M_ACTION_RUNNING_DOWN_LEFT_SHOOT = M_ACTION_RUNNING_UP_RIGHT_SHOOT + 1;
	private final int M_ACTION_RUNNING_DOWN_RIGHT_SHOOT = M_ACTION_RUNNING_DOWN_LEFT_SHOOT + 1;
	private final int M_ACTION_JUMPING_LEFT_SHOOT = M_ACTION_RUNNING_DOWN_RIGHT_SHOOT + 1;
	private final int M_ACTION_JUMPING_RIGHT_SHOOT = M_ACTION_JUMPING_LEFT_SHOOT + 1;
	private final int M_ACTION_LYING_LEFT_SHOOT = M_ACTION_JUMPING_RIGHT_SHOOT + 1;
	private final int M_ACTION_LYING_RIGHT_SHOOT = M_ACTION_LYING_LEFT_SHOOT + 1;
	private final int M_ACTION_MAX = M_ACTION_LYING_RIGHT_SHOOT + 1;//30
	
	//control
	private int m_move_static_circle_x, m_move_static_circle_y;	
	private float m_move_touched_posx, m_move_touched_posy;
	private int m_jump_button_centerx, m_jump_button_centery;
	private int m_fire_button_centerx, m_fire_button_centery;
	private int m_button_radius;
	private int m_move_pointer_id, m_move_pointer_index;
	private int m_fire_pointer_id, m_fire_pointer_index;
	private int m_jump_pointer_id, m_jump_pointer_index;
	private double m_move_distance, m_move_angle;
	private final int M_DOWN_ANGLE = 23;
	private final int M_MOVE_DOWN_ANGLE = 67;
	private final int M_MOVE_UP_ANGLE = 113;
	private final int M_UP_ANGLE = 157;	
	
	//debug
	private int m_pointer_count;
	private int [] pointer_x, pointer_y, pointer_id;
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);
		s_screen_width = context.getResources().getDisplayMetrics().widthPixels;
		s_screen_height = context.getResources().getDisplayMetrics().heightPixels;
		m_assetmanager = context.getAssets();
	}

	private void Initialize(){
		
		try{
			m_inputstream = m_assetmanager.open("gfx/ContraSheet.png");
			m_mc_bitmap = BitmapFactory.decodeStream(m_inputstream);
//			m_ryu_bitmap_transparent = makeTransparent(m_ryu_bitmap);
//			m_ryu_animation = new Animation(m_ryu_bitmap, 77, 65, 68, 66, 13, 0, false, false, false, false, 100);
//			m_inputstream = m_assetmanager.open("gfx/bird_anims.png");
//			m_birds_bitmap = BitmapFactory.decodeStream(m_inputstream);
//			m_red_bird_animation = new Animation(m_birds_bitmap, 41, 124, 34, 52, 3, 0, true, true, true, true, 200);
//			m_yellow_bird_animation = new Animation(m_birds_bitmap, 21, 299, 34, 24, 3, 22, false, true, true, false, 20);
			
			m_inputstream.close();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.toString());
			m_inputstream = null;
			m_mc_bitmap = null;
//			m_ryu_bitmap_transparent = null;
//			m_ryu_animation = null;
//			m_birds_bitmap = null;
//			m_red_bird_animation = null;
//			m_yellow_bird_animation = null;
		}
		
		
		//mc action animation list
		m_mc_animations = new Animation[M_ACTION_MAX];
//		m_action_standing_left_array = {};
		
		m_paint = new Paint();
		m_mc_posy = s_screen_height/2 - 10;
		m_move_static_circle_x = s_screen_width/7;		
		m_move_static_circle_y = s_screen_height - s_screen_height/4;
		m_move_touched_posx = s_screen_width/7;
		m_move_touched_posy = s_screen_height - s_screen_height/4;
		m_jump_button_centerx = s_screen_width - s_screen_width/12;
		m_jump_button_centery = s_screen_height/2 + s_screen_height/8;
		m_fire_button_centerx = s_screen_width - s_screen_width/12;
		m_fire_button_centery = s_screen_height - s_screen_height/8;;
		m_button_radius = s_screen_height/10;
		m_move_pointer_id = -1;
		m_fire_pointer_id = -1;
		m_jump_pointer_id = -1;
		m_move_pointer_index = -1;
		m_fire_pointer_index = -1;
		m_jump_pointer_index = -1;
		m_move_distance = 0;
		m_move_angle = 0;		
		m_mc_posx = s_screen_width/2;
		m_mc_posy = s_screen_height/2;
		m_mc_current_action = -1;;
		m_mc_is_face_left = false;
		m_mc_is_firing = false;
		
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
	}
	
	public void GameCreate(){
	}
	
	public void GameStart(){
	}
	
	public void GamePause(){
	}
	
	public void GameResume(){
	}
	
	public void GameStop(){
	}
	
	public void GameDestroy(){
	}
	
	public void GameRestart(){
	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		m_paint.reset();
		int debug_string_y = 0;
		canvas.drawColor(Color.WHITE);
		m_paint.setColor(Color.LTGRAY);
		canvas.drawRect(0, getHeight()/2, getWidth(), getHeight(), m_paint);		
		m_paint.setAntiAlias(true);		
		m_paint.setColor(Color.RED);
		canvas.drawCircle(m_fire_button_centerx, m_fire_button_centery, m_button_radius, m_paint);
		m_paint.setColor(Color.BLUE);
		canvas.drawCircle(m_jump_button_centerx, m_jump_button_centery, m_button_radius, m_paint);		
		m_paint.setStyle(Style.STROKE);
		m_paint.setStrokeWidth(5);
		m_paint.setColor(Color.YELLOW);
		canvas.drawCircle(m_move_touched_posx, m_move_touched_posy, m_button_radius, m_paint);
		if((m_move_touched_posx == m_move_static_circle_x) && (m_move_touched_posy == m_move_static_circle_y)){
			m_paint.setColor(Color.GREEN);
			canvas.drawCircle(m_move_static_circle_x, m_move_static_circle_y, m_button_radius, m_paint);
		}
		m_paint.setStrokeWidth(2);
		m_paint.setColor(Color.BLACK);
		canvas.drawCircle(m_move_static_circle_x, m_move_static_circle_y, m_button_radius*2, m_paint);
		
		//draw mc
//		m_mc_animations[get_MC_Action()].render(canvas, m_mc_posx, m_mc_posy);
		
		//debug
		m_paint.setStrokeWidth(0);
		m_paint.setColor(Color.BLACK);
		canvas.drawText("Move: " + m_move_pointer_id + ", " + m_move_pointer_index, 5, debug_string_y += 15, m_paint);
		canvas.drawText("Fire: " + m_fire_pointer_id + ", " + m_fire_pointer_index, 5, debug_string_y += 15, m_paint);
		canvas.drawText("Jump: " + m_jump_pointer_id + ", " + m_jump_pointer_index, 5, debug_string_y += 15, m_paint);
		canvas.drawText("Move angle: " + Math.toDegrees(m_move_angle), 5, debug_string_y += 15, m_paint);
		for(int i = 0; i < m_pointer_count; i ++){
			canvas.drawText(i + ", " + pointer_id[i] + ", " + pointer_x[i] + ", " + pointer_y[i], 5, debug_string_y += 15, m_paint);
		}
		if(m_move_pointer_index != -1){
			canvas.drawText("Moving " + m_move_pointer_id, 5, debug_string_y += 15, m_paint);
		}
		if(m_fire_pointer_index != -1){
			canvas.drawText("Firing " + m_fire_pointer_id, 5, debug_string_y += 15, m_paint);
		}
		if(m_jump_pointer_index != -1){
			canvas.drawText("Jumping " + m_jump_pointer_id, 5, debug_string_y += 15, m_paint);
		}		
		canvas.drawLine(0, 0, m_mc_posx, m_mc_posy, m_paint);
	}
	
	private boolean CheckPointInCircle(int point_x, int point_y, int circle_heart_x, int circle_heart_y, int radius){
		double distance = Math.sqrt(Math.pow((circle_heart_x - point_x), 2) + Math.pow((circle_heart_y - point_y), 2));
		if(distance <= radius)
			return true;
		return false;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		m_pointer_count = e.getPointerCount();
		if(e.getActionMasked() == MotionEvent.ACTION_UP || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP){
			
			if(e.getActionIndex() == m_move_pointer_index){
				//find and set new pointer if have pointer enough condition
				for(int i = 0; i < m_pointer_count; i ++){
					if(e.getPointerId(i) != m_move_pointer_id && e.getX(i) < s_screen_width/2){
						//have another pointer enough condition
						m_move_pointer_index = i;
						m_move_pointer_id = e.getPointerId(i);
						break;
					}else{
						//pointer not enough condition
						m_move_pointer_index = -1;
						m_move_pointer_id = -1;
						m_move_touched_posx = m_move_static_circle_x;
						m_move_touched_posy = m_move_static_circle_y;
					}
				}				
			}
			if(e.getActionIndex() == m_fire_pointer_index){
				//find and set new pointer if have pointer enough condition
				for(int i = 0; i < m_pointer_count; i ++){
					if(e.getPointerId(i) != m_fire_pointer_id && CheckPointInCircle((int)e.getX(e.getActionIndex()), (int)e.getY(e.getActionIndex()), m_fire_button_centerx, m_fire_button_centery, m_button_radius)){
						//have another pointer enough condition
						m_fire_pointer_index = i;
						m_fire_pointer_id = e.getPointerId(i);
						set_MC_Firing(true);
						break;
					}else{
						//pointer not enough condition						
						m_fire_pointer_index = -1;
						m_fire_pointer_id = -1;
						set_MC_Firing(false);
					}
				}				
			}
			if(e.getActionIndex() == m_jump_pointer_index){
				//find and set new pointer if have pointer enough condition
				for(int i = 0; i < m_pointer_count; i ++){
					if(e.getPointerId(i) != m_jump_pointer_id && CheckPointInCircle((int)e.getX(e.getActionIndex()), (int)e.getY(e.getActionIndex()), m_jump_button_centerx, m_jump_button_centery, m_button_radius)){
						//have another pointer enough condition
						m_jump_pointer_index = i;
						m_jump_pointer_id = e.getPointerId(i);
						set_MC_Action(M_ACTION_JUMPING);
						break;
					}else{
						//pointer not enough condition
						m_jump_pointer_index = -1;
						m_jump_pointer_id = -1;
						set_MC_Action(M_ACTION_STANDING);
					}
				}				
			}
		}
		
		if(e.getActionMasked() == MotionEvent.ACTION_DOWN || e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
			//get pointer id and index if enough condition			
			if(m_move_pointer_id == -1 && e.getX(e.getActionIndex()) < s_screen_width/2){
				m_move_pointer_index = e.getActionIndex();
				m_move_pointer_id = e.getPointerId(m_move_pointer_index);
			}else{
				if(m_fire_pointer_id == -1 && CheckPointInCircle((int)e.getX(e.getActionIndex()), (int)e.getY(e.getActionIndex()), m_fire_button_centerx, m_fire_button_centery, m_button_radius)){
					m_fire_pointer_index = e.getActionIndex();
					m_fire_pointer_id = e.getPointerId(m_fire_pointer_index);
					set_MC_Firing(true);
				}
				if(m_jump_pointer_id == -1 && CheckPointInCircle((int)e.getX(e.getActionIndex()), (int)e.getY(e.getActionIndex()), m_jump_button_centerx, m_jump_button_centery, m_button_radius)){
					m_jump_pointer_index = e.getActionIndex();
					m_jump_pointer_id = e.getPointerId(m_jump_pointer_index);
					//TODO: temp code here
					if(get_MC_Current_Action() != M_ACTION_JUMPING)
						set_MC_Action(M_ACTION_JUMPING);
					else
						set_MC_Action(M_ACTION_STANDING);
				}				
			}
		}
		
		if(e.getActionMasked() == MotionEvent.ACTION_MOVE){
			//update follow pointer id
			//move
			m_move_pointer_index = e.findPointerIndex(m_move_pointer_id);
			if(m_move_pointer_index != -1){
				if(e.getX(m_move_pointer_index) < s_screen_width/2){
					m_move_touched_posx = e.getX(m_move_pointer_index);
					m_move_touched_posy = e.getY(m_move_pointer_index);
					m_move_distance = Math.sqrt(Math.pow(m_move_touched_posx - m_move_static_circle_x, 2) + Math.pow(m_move_touched_posy - m_move_static_circle_y, 2));
					m_move_angle = Math.atan2(m_move_touched_posx - m_move_static_circle_x, m_move_touched_posy - m_move_static_circle_y);									
					if(m_move_distance > m_button_radius*2){
						m_move_distance = m_button_radius*2;					
					}				
					m_move_touched_posx = (float)(m_move_static_circle_x + m_move_distance * Math.sin(m_move_angle));
					m_move_touched_posy = (float)(m_move_static_circle_y + m_move_distance * Math.cos(m_move_angle));
					//set mc action
					if(Math.toDegrees(m_move_angle) < 0){
						set_MC_Face_Left(true);
						m_move_angle *= -1;
					}else{
						set_MC_Face_Left(false);
					}
					//set MC action
					if(get_MC_Current_Action() != M_ACTION_JUMPING && get_MC_Current_Action() != M_ACTION_DIEING){
						if(m_move_angle <= M_DOWN_ANGLE){
							set_MC_Action(M_ACTION_LYING);
						}else if(m_move_angle > M_DOWN_ANGLE && m_move_angle <= M_MOVE_DOWN_ANGLE){
							set_MC_Action(M_ACTION_RUNNING_DOWN);
						}else if(m_move_angle > M_MOVE_DOWN_ANGLE && m_move_angle <= M_MOVE_UP_ANGLE){
							set_MC_Action(M_ACTION_RUNNING);
						}else if(m_move_angle > M_MOVE_UP_ANGLE && m_move_angle <= M_UP_ANGLE){
							set_MC_Action(M_ACTION_RUNNING_UP);
						}else if(m_move_angle > M_UP_ANGLE){
							set_MC_Action(M_ACTION_STANDING_UP);
						}
					}
					
				}else{
					set_MC_Action(M_ACTION_STANDING);
					m_move_pointer_index = -1;
					m_move_pointer_id = -1;
					m_move_touched_posx = m_move_static_circle_x;
					m_move_touched_posy = m_move_static_circle_y;
				}
			}
			//fight
			m_fire_pointer_index = e.findPointerIndex(m_fire_pointer_id);
			if(m_fire_pointer_index != -1){
				if(CheckPointInCircle((int)e.getX(m_fire_pointer_index), (int)e.getY(m_fire_pointer_index), m_fire_button_centerx, m_fire_button_centery, m_button_radius)){
					//pointer moving on button fire
					set_MC_Firing(true);
				}else{					
					m_fire_pointer_index = -1;
					m_fire_pointer_id = -1;
					set_MC_Firing(false);
				}
			}
			//jump
			m_jump_pointer_index = e.findPointerIndex(m_jump_pointer_id);
			if(m_jump_pointer_index != -1){
				if(CheckPointInCircle((int)e.getX(m_jump_pointer_index), (int)e.getY(m_jump_pointer_index), m_jump_button_centerx, m_jump_button_centery, m_button_radius)){
					//pointer moving on button jump
					//TODO: correct code here
					set_MC_Action(M_ACTION_JUMPING);
				}else{					
					m_jump_pointer_index = -1;
					m_jump_pointer_id = -1;
					set_MC_Action(M_ACTION_STANDING);
				}
			}
			
			//refresh pointer id and index if enough condition
			for(int i = 0; i < m_pointer_count; i ++){
				if(m_move_pointer_id == -1 && e.getX(i) < s_screen_width/2){
					m_move_pointer_index = i;
					m_move_pointer_id = e.getPointerId(m_move_pointer_index);
					break;
				}else{
					if(m_fire_pointer_id == -1 && CheckPointInCircle((int)e.getX(i), (int)e.getY(i), m_fire_button_centerx, m_fire_button_centery, m_button_radius)){
						m_fire_pointer_index = i;
						m_fire_pointer_id = e.getPointerId(m_fire_pointer_index);
						break;
					}
					if(m_jump_pointer_id == -1 && CheckPointInCircle((int)e.getX(i), (int)e.getY(i), m_jump_button_centerx, m_jump_button_centery, m_button_radius)){
						m_jump_pointer_index = i;
						m_jump_pointer_id = e.getPointerId(m_jump_pointer_index);
						break;
					}				
				}
			}
		}
		m_pointer_count = e.getPointerCount();
		pointer_x = new int[m_pointer_count];
		pointer_y = new int[m_pointer_count];
		pointer_id = new int[m_pointer_count];		
		for(int i = 0; i < e.getPointerCount(); i ++){
			pointer_x[i] = (int)e.getX(i);
			pointer_y[i] = (int)e.getY(i);
			pointer_id[i] = (int)e.getPointerId(i);
		}
		return true;
	}
	
	private void Jump_Update(){
		if(get_MC_Current_Action() == M_ACTION_JUMPING){
			
		}
	}
	
	private void Dead_Update(){
		if(get_MC_Current_Action() == M_ACTION_DIEING){
			
		}
	}
	
	private void set_MC_Action(int main_action){
		m_mc_current_action = main_action;
	}
	
	private int get_MC_Current_Action(){
		return m_mc_current_action;
	}
	
	private void set_MC_Face_Left(boolean need_turn_mc_face_to_left_side){
		m_mc_is_face_left = need_turn_mc_face_to_left_side;
	}
	
	private void set_MC_Firing(boolean is_firing){
		m_mc_is_firing = is_firing;
	}
	
	private int get_MC_Action(){
		switch(m_mc_current_action){
			case M_ACTION_STANDING:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_STANDING_LEFT_SHOOT;
					}else{
						return M_ACTION_STANDING_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_STANDING_RIGHT_SHOOT;
					}else{
						return M_ACTION_STANDING_RIGHT;
					}
				}
			}
			case M_ACTION_STANDING_UP:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){						
						return M_ACTION_STANDING_UP_LEFT_SHOOT;
					}else{
						return M_ACTION_STANDING_UP_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_STANDING_UP_RIGHT_SHOOT;
					}else{
						return M_ACTION_STANDING_UP_RIGHT;
					}
				}
			}
			case M_ACTION_RUNNING:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_LEFT_SHOOT;
					}else{
						return M_ACTION_RUNNING_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_RIGHT_SHOOT;
					}else{
						return M_ACTION_RUNNING_RIGHT;
					}
				}
			}
			case M_ACTION_RUNNING_UP:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_LEFT_SHOOT;
					}else{
						return M_ACTION_RUNNING_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_RIGHT_SHOOT;
					}else{
						return M_ACTION_RUNNING_RIGHT;
					}
				}
			}
			case M_ACTION_RUNNING_DOWN:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_UP_LEFT_SHOOT;
					}else{
						return M_ACTION_RUNNING_UP_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_RUNNING_DOWN_RIGHT_SHOOT;
					}else{
						return M_ACTION_RUNNING_DOWN_RIGHT;
					}
				}
			}
			case M_ACTION_JUMPING:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_JUMPING_LEFT_SHOOT;
					}else{
						return M_ACTION_JUMPING_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_JUMPING_RIGHT_SHOOT;
					}else{
						return M_ACTION_JUMPING_RIGHT;
					}
				}
			}
			case M_ACTION_LYING:{
				if(m_mc_is_face_left){
					if(m_mc_is_firing){
						return M_ACTION_LYING_LEFT_SHOOT;
					}else{
						return M_ACTION_LYING_LEFT;
					}						
				}else{
					if(m_mc_is_firing){
						return M_ACTION_LYING_RIGHT_SHOOT;
					}else{
						return M_ACTION_LYING_RIGHT;
					}
				}
			}
			case M_ACTION_DIEING:{
				if(m_mc_is_face_left){
					return M_ACTION_DIEING_LEFT;	
				}else{
					return M_ACTION_DIEING_RIGHT;
				}
			}
			default:
				return M_ACTION_STANDING_RIGHT;
		}
	}

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
